using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace WebUpdaterAdminCore.Utils
{
    public static class Common
    {
        public static string FileMd5(string filePath)
        {
            if(!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                using(var file = File.OpenRead(filePath))
                {
                    return FileMd5(file);
                }
            }
            else
            {
                return null;
            }
        }

        public static string FileMd5(Stream stream)
        {
            if (stream == null) return null;
            if(stream.CanSeek && stream.Position != 0)
            {
                stream.Position = 0;
            }
            var hash = MD5.Create().ComputeHash(stream);
            return BitConverter.ToString(hash).Replace("-", "");
        }
    }
}
